package com.t.bricks.bussiness.db.dao.loginuser.impl;

import java.util.Properties;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.loginuser.LoginUserDao;
import com.t.bricks.bussiness.db.entity.loginuser.LoginUser;
import com.t.bricks.bussiness.db.mapper.loginuser.LoginUserMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class LoginUserDaoImpl implements LoginUserDao<LoginUser> {
	
	@Autowired
	private LoginUserMapper loginUserMapper;
	
	@Autowired
    private SqlSessionFactory sqlSessionFactory;
	
	/**
	 * 是否使用dev库, 0:未检测;1:是;2:否
	 */
	private int iDev = 0;

	/**
	 * 修改密码
	 * @param uId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param sModifieder
	 * @param sOwner
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity editPass(String uId, String sOldPass, String sNewPass,
			int iVersion, String sModifieder, String sOwner) {
		try {
			int i = loginUserMapper.editPass(uId, sOldPass, sNewPass, iVersion, sModifieder);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 7001);
			}
			
			if (null == sOwner || "".equals(sOwner.trim()) || "BricksBaseSystem.LoginUser".equals(sOwner.trim())) {
				return new MsgEmity(true, "修改成功!", 7999);
			}
			
			if (iDev == 0) {
				Configuration configuration = sqlSessionFactory.getConfiguration();
				Properties variables = configuration.getVariables();
				
				String dbName = variables.getProperty("BricksBaseSystem");//查配置文件中的变量'mybatis.configuration.variables.BricksBaseSystem'
				if (null == dbName || "".equals(dbName)) {
					return new MsgEmity(false, "'用户来源库'格式错误,需为'库名.表名'", 7001);
				}
				
				iDev = (dbName.toUpperCase().startsWith("DEV_")) ? 1 : 2;
			}
			
			if (iDev == 1 && !sOwner.toUpperCase().startsWith("DEV_")) {//使用开发库
				sOwner = String.format("Dev_%s", sOwner);
			}
			
			i = loginUserMapper.editPassByOwner(uId, sOldPass, sNewPass, sModifieder, sOwner);
			if (i < 1) {
				Log.error("修改来源表中的密码未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改来源表中的密码失败!", 7003);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "修改时发生异常!", 7002);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}
	
	/**
	 * 根据用户编号设置用户类型
	 * @param uId 用户编号
	 * @param sType 用户类型
	 * @return
	 */
	@Override
	public MsgEmity editUserType(String uId, Integer sType) {
		try {
			int i = loginUserMapper.editUserType(uId, sType);
			if (i < 1) {
				LoginUser loginUserData = new LoginUser();
				loginUserData.setuId(uId);
				int iCount = loginUserMapper.findCountByKey(loginUserData);
				if (iCount < 1) {
					return new MsgEmity(false, "指定用户不存在!", 7001);
				}
				
				Log.error("设置未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "设置失败!", 7002);
			}
		} catch (Exception e) {
			Log.error("设置时发生异常:", e.getMessage());
			return new MsgEmity(false, "设置时发生异常!", 7003);
		}
		
		return new MsgEmity(true, "设置成功!", 7999);
	}

}
